Skip to content

Conversation

@AndyBodnar
Copy link

Summary

Adds a Rust equivalent of the JavaScript convertFileSrc function to the PathResolver API. This enables file-to-URL conversion from Rust code, which is useful when processing file paths in Rust (e.g., parsing markdown with image references) and needing to convert them to URLs that the webview can load.

The implementation:

  • Uses percent-encoding for URL-safe path encoding (already a dependency)
  • Uses dunce::simplified to normalize Windows paths (already a dependency)
  • Returns HTTP URLs on Windows/Android and custom protocol URLs elsewhere, matching the JS implementation

Usage

use tauri::Manager;

tauri::Builder::default()
    .setup(|app| {
        let video_path = app.path().app_data_dir()?.join("video.mp4");
        let url = app.path().convert_file_src(&video_path, None);
        // On Windows: http://asset.localhost/C%3A%5CUsers%5C...
        // On macOS/Linux: asset://localhost/%2FUsers%2F...
        println!("URL: {}", url);
        Ok(())
    });

Test plan

  • Added function to PathResolver in desktop.rs
  • Added function to PathResolver in android.rs
  • Verify build passes in CI

Closes #12022

@AndyBodnar AndyBodnar requested a review from a team as a code owner January 17, 2026 06:43
@github-project-automation github-project-automation bot moved this to 📬Proposal in Roadmap Jan 17, 2026
@AndyBodnar
Copy link
Author

This adds the Rust equivalent of the JavaScript convertFileSrc function. Followed the existing patterns in the codebase:

  • Desktop version uses dunce::simplified for path canonicalization and percent-encodes the path
  • Android version uses the same approach (HTTP protocol like Windows)
  • iOS version also added with the same pattern

Platform-specific URL schemes:

  • Windows/Android: http://{protocol}.localhost/{encoded_path}
  • macOS/Linux/iOS: {protocol}://localhost/{encoded_path}

Socket Security checks passed.

@AndyBodnar
Copy link
Author

All checks passed.

What this adds

The Rust equivalent of the JavaScript convertFileSrc() function. This lets you convert a local file path into a URL that can be loaded by the webview, which is useful for displaying local images, videos, or other assets.

Implementation details

  • Added convert_file_src() to PathResolver for desktop, Android, and iOS
  • Uses dunce::simplified() on desktop to clean up Windows UNC paths
  • Percent-encodes the path to handle special characters and spaces
  • Returns platform-appropriate URLs:
    • Windows/Android: http://{protocol}.localhost/{encoded_path}
    • macOS/Linux/iOS: {protocol}://localhost/{encoded_path}
  • Default protocol is "asset" but can be customized

Why this matters

Previously you had to use the JS bridge to call convertFileSrc() from Rust code, or manually construct the URL. This gives Rust-side code a clean way to generate asset URLs without going through JS.

Testing

  • Socket Security checks passed
  • Followed the existing patterns in the codebase for path handling and URL construction

@AndyBodnar

This comment was marked as spam.

Copy link
Member

@FabianLars FabianLars left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! On top of the https comments, i need you to sign your commits to be able to merge the PR. Lastly, a small changefile will be needed (check .changes dir).

Thanks again :)

@github-project-automation github-project-automation bot moved this from 📬Proposal to 🏗 In progress in Roadmap Jan 19, 2026
@FabianLars FabianLars added the status: waiting Waiting on author label Jan 20, 2026
Adds a Rust equivalent of the JavaScript `convertFileSrc` function to the
PathResolver API, enabling file-to-URL conversion from Rust code.

This is useful when processing file paths in Rust (e.g., parsing markdown
with image references) and needing to convert them to URLs that the webview
can load.

The implementation:
- Uses percent-encoding for URL-safe path encoding
- Uses dunce::simplified to normalize Windows paths
- Returns HTTP URLs on Windows/Android and custom protocol URLs elsewhere
- Adds `use_https` parameter to support webviews with `use_https_scheme` enabled

Closes tauri-apps#12022
@AndyBodnar AndyBodnar force-pushed the feat/convert-file-src-rust branch from 70b74d0 to 52154ed Compare January 22, 2026 03:32
@AndyBodnar
Copy link
Author

Added the use_https parameter to convert_file_src so it works with webviews that have use_https_scheme enabled. Passing Some(true) will generate https:// URLs instead of http:// on Windows and Android. Also added the changefile and signed the commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting Waiting on author

Projects

Status: 🏗 In progress

Development

Successfully merging this pull request may close these issues.

[feat] Rust equivalent convertFileSrc()

2 participants